/**
 * @author Can-Chen
 * @description 节点遍历器
 */

const nodeHandler = require('./es_v');
const Scope = require('./scope');

function NodeIterator(node, scope) {
    /**
     * @desc 节点
     */
    this.node = node;

    /**
     * @desc 
     */
    this.scope = scope;

    /**
     * @desc 节点处理器，处理AST当中的节点
     */
    this.nodeHandler = nodeHandler;
}

NodeIterator.prototype = {

    ...NodeIterator.prototype,

    traverse: function (node, options = {}) {
        const scope = options.scope || this.scope;
        const nodeIterator = new NodeIterator(node, scope);
        const _eval = this.nodeHandler[node.type];

        if (!_eval) {
            throw new Error(`Unknown node type ${node.type}`)
        }

        return _eval(nodeIterator)
    },

    createScope: function (blockType = 'block') {

        return new Scope(blockType, this.scope)
    }
}

module.exports = NodeIterator;